﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Microhard.Framework;
using Microhard.Permission;
using Microhard.Permission.Models;
using Microhard.Permission.Interface;
using Microsoft.Practices.Unity;
using FluentValidation.Mvc;
using Microhard.Framework.MVCHelper;
using Microhard.Core.Configuration;
using Microhard.Permission.MVCFilter;
using Microhard.WebUI.Models;



namespace Microhard.WebUI.Controllers
{
    public class AccountController : Controller
    {
        [Dependency]
        public IUser user { set; get; }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
            
        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login([CustomizeValidator(RuleSet = "Login")]Logon model)
        {
            if (ModelState.IsValid)
            {
                if (user.ValidatUser(model.UserName, model.Password))
                {
                    user.SignIn(model.UserName, model.RememberMe);
                    this.HttpContext.Session["userInfo"] = user.GetUserByName(model.UserName);
                    return RedirectToAction("Welcome", "Account");
                }
                ModelState.AddModelError("", "提供的用户名或密码不正确。");
            }
            return View(model);
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOff()
        {
            user.SignOut();
            this.HttpContext.Session.Clear();
            return RedirectToAction("Login");
        }

        /// <summary>
        /// 管理员欢迎
        /// </summary>
        /// <returns></returns>
        [MVCFilterAtrForLogon()]
        public ActionResult Welcome()
        {
            return View();
        }


        
    }


}
